//
//  TextFieldStyle.swift
//  Alamofire
//
//  Created by Fanxx on 2019/7/8.
//

import UIKit

extension UCSetting where TargetType: UITextField {
    ///文本
    public static func text(_ text:String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.text = text
        })
    }
    public static func text(_ text:NSAttributedString?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.attributedText = text
        })
    }
    ///占位文本
    public static func placeholder(_ text:String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.placeholder = text
        })
    }
    ///占位文本
    public static func placeholder(_ text:NSAttributedString?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.attributedPlaceholder = text
        })
    }
    ///字体
    public static func font(_ font:UIFont?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.font = font
        })
    }
    ///系统字体
    public static var systemFont:UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: UIFont.systemFontSize))}
    ///系统字体
    public static func font(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size))}
    ///系统字体
    public static func font(size: CGFloat, weight: UIFont.Weight) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size, weight: weight))}
    ///系统字体
    public static func boldFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.boldSystemFont(ofSize: size))}
    ///系统字体
    public static func italicFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.italicSystemFont(ofSize: size))}
    
    ///文本颜色
    public static func color(_ color:UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textColor = color
        })
    }
    ///对齐
    public static func align(_ align:NSTextAlignment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textAlignment = align
        })
    }
    ///居中对齐
    public static var center: UCSetting<TargetType> {
        return self.align(.center)
    }
    ///左对齐
    public static var left: UCSetting<TargetType> {
        return self.align(.left)
    }
    ///右对齐
    public static var right: UCSetting<TargetType> {
        return self.align(.right)
    }
    ///字体最小比例
    public static func min(scale:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsFontSizeToFitWidth = false
            target.minimumFontSize = scale
        })
    }
    ///字体最小尺寸
    public static func min(size:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsFontSizeToFitWidth = true
            target.minimumFontSize = size
        })
    }
    ///清除模式
    public static func clear(model:UITextField.ViewMode = .whileEditing) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.clearButtonMode = model
        })
    }
    ///是否隐藏输入，默认true
    public static func secure(_ secure:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isSecureTextEntry = secure
        })
    }
    ///键盘
    public static func keyboard(_ type:UIKeyboardType) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.keyboardType = type
        })
    }
    ///返回键
    public static func returnKey(_ type:UIReturnKeyType) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.returnKeyType = type
        })
    }
    ///borderStyle
    public static func style(border style:UITextField.BorderStyle) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.borderStyle = style
        })
    }
    ///defaultTextAttributes
    public static func text(attr: [NSAttributedString.Key : Any]) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.defaultTextAttributes = attr
        })
    }
    ///clearsOnBeginEditing
    public static func clear(onEdit enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.clearsOnBeginEditing = enable
        })
    }
    ///adjustsFontSizeToFitWidth
    public static func font(adjust enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsFontSizeToFitWidth = enable
        })
    }
    ///minimumFontSize
    public static func min(font size: CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.minimumFontSize = size
        })
    }
    ///delegate
    public static func delegate(_ delegate: UITextFieldDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.delegate = delegate
        })
    }
    ///background
    public static func bg(_ image: UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.background = image
        })
    }
    ///disabledBackground
    public static func bg(disable image: UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.disabledBackground = image
        })
    }
    ///allowsEditingTextAttributes
    public static func editing(attr enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsEditingTextAttributes = enable
        })
    }
    ///typingAttributes
    public static func typing(attr: [NSAttributedString.Key : Any]) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.typingAttributes = attr
        })
    }
    ///leftView
    public static func left(_ view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.leftView = view
        })
    }
    ///leftViewMode
    public static func left(mode: UITextField.ViewMode) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.leftViewMode = mode
        })
    }
    ///rightView
    public static func right(_ view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.rightView = view
        })
    }
    ///rightViewMode
    public static func right(mode: UITextField.ViewMode) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.rightViewMode = mode
        })
    }
    ///自定义键盘 inputView
    public static func input(_ view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.inputView = view
        })
    }
    ///自定义键盘 inputAccessoryView
    public static func input(accessory view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.inputAccessoryView = view
        })
    }
    ///clearsOnInsertion
    public static func clearOnInsert(_ enable: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.clearsOnInsertion = enable
        })
    }
}
